#ifndef USERSESSION_H
#define USERSESSION_H

#include <QObject>
#include <QString>
#include <QDateTime>
#include <QTimer>

// User role enumeration
typedef enum {
    USER_ROLE_ADMIN = 0,    // Administrator
    USER_ROLE_OPERATOR,     // Operator
    USER_ROLE_VIEWER        // Viewer
} UserRole;

// User information structure
typedef struct {
    QString username;       // Username
    QString fullName;       // Full name
    UserRole role;          // User role
    QDateTime lastLogin;    // Last login time
    bool isActive;          // Is user active
    QString email;          // Email address
    QDateTime createdTime;  // Account creation time
} UserInfo;

class UserSession : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool isLoggedIn READ isLoggedIn NOTIFY loginStatusChanged)
    Q_PROPERTY(QString currentUser READ getCurrentUser NOTIFY currentUserChanged)
    Q_PROPERTY(QString currentUserRole READ getCurrentUserRole NOTIFY currentUserChanged)

public:
    explicit UserSession(QObject *parent = nullptr);
    ~UserSession();

    // Authentication methods
    bool authenticateUser(const QString& username, const QString& password);
    void logoutUser();
    bool isLoggedIn() const;
    
    // User information methods
    QString getCurrentUser() const;
    QString getCurrentUserRole() const;
    UserRole getCurrentUserRoleEnum() const;
    UserInfo getCurrentUserInfo() const;
    
    // Session management
    void refreshSession();
    bool isSessionValid() const;
    int getSessionTimeoutMinutes() const;
    void setSessionTimeoutMinutes(int minutes);
    
    // User management methods
    bool createUser(const QString& username, const QString& password, 
                   const QString& fullName, UserRole role);
    bool deleteUser(const QString& username);
    bool changeUserPassword(const QString& username, const QString& newPassword);
    bool updateUserInfo(const QString& username, const UserInfo& userInfo);
    QList<UserInfo> getAllUsers() const;
    
    // Password validation
    bool validatePassword(const QString& password) const;
    QString getPasswordRequirements() const;

signals:
    void loginStatusChanged(bool isLoggedIn);
    void currentUserChanged(const QString& username);
    void sessionTimeout();
    void userCreated(const QString& username);
    void userDeleted(const QString& username);
    void userUpdated(const QString& username);

private slots:
    void onSessionTimeout();

private:
    // Private methods
    bool loadUserDatabase();
    bool saveUserDatabase();
    QString hashPassword(const QString& password, const QString& salt) const;
    QString generateSalt() const;
    bool verifyPassword(const QString& password, const QString& hash, const QString& salt) const;
    UserRole stringToUserRole(const QString& roleStr) const;
    QString userRoleToString(UserRole role) const;
    void startSessionTimer();
    void stopSessionTimer();
    void clearSession();

    // Member variables
    bool m_isLoggedIn;
    QString m_currentUser;
    UserInfo m_currentUserInfo;
    QDateTime m_loginTime;
    QDateTime m_lastActivity;
    int m_sessionTimeoutMinutes;
    QTimer* m_sessionTimer;
    QList<UserInfo> m_users;
    QString m_userDatabasePath;
    QString m_userId;
    QString m_userName;
};

#endif // USERSESSION_H